package com.apotik.legenda.util;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class MyTableModel  extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	
	private String[] columnNames;
	
	@SuppressWarnings("rawtypes")
	private Vector data = new Vector();
	
	
	public MyTableModel(String[] columnNames) {
		this.columnNames = columnNames;
	}


	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getValueAt(int rowIndex, int colIndex) {
		Vector vector = (Vector) data.get(rowIndex);
		return vector.get(colIndex);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		Vector vector = (Vector) data.get(rowIndex);
		vector.setElementAt(value, columnIndex);
		fireTableCellUpdated(rowIndex, columnIndex);
	}
	
	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void insertData(Object[] values) {
		Vector vector = new Vector();
		data.add(vector);
		
		for (int i = 0; i < values.length; i++) {
			vector = (Vector) data.get(data.size() - 1);
			vector.add(values[i]);
		}
		
		fireTableDataChanged();
	}

	public void removeRow(int row) {
		data.removeElementAt(row);
		fireTableDataChanged();
	}
	
}
